package com.fengmi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.fengmi.dto.PageDTO;
import com.fengmi.entity.MallGoodsBrand;
import com.fengmi.entity.SysPermission;
import com.fengmi.mapper.SysPermissionMapper;
import com.fengmi.service.SysPermissionService;
import com.fengmi.vo.CascadeVO;
import com.fengmi.vo.PageResultVO;
import com.fengmi.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class SysPermissionServiceImpl implements SysPermissionService {

    @Autowired
    private SysPermissionMapper permissionMapper;

    @Override
    public PageResultVO<SysPermission> findPermissionByPage(PageDTO<SysPermission> pageDTO) {
        //非空判断
        if (pageDTO == null) {
            return new PageResultVO<>(false, "参数不合法");
        }

        //当前页
        Integer pageNum = pageDTO.getPageNum();
        //每页记录数
        Integer pageSize = pageDTO.getPageSize();
        //进行非空判断
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 5;
        }
        //开始分页
        PageHelper.startPage(pageNum, pageSize);
        //调用mapper层查询
        List<SysPermission> permissionList = permissionMapper.findPermissionByWhere(pageDTO.getWhere());
        //分页信息对象创建，放入查询集合
        PageInfo<SysPermission> pageInfo = new PageInfo<>(permissionList);
        //创建返回对象
        PageResultVO<SysPermission> pageResultVO = new PageResultVO<>(true, "查询成功");
        //设置每页数据
        pageResultVO.setData(pageInfo.getList());
        //设置总页数
        pageResultVO.setTotalPage(pageInfo.getPages());
        //设置总记录数
        pageResultVO.setTotalSize(pageInfo.getTotal());

        return pageResultVO;
    }

    @Override
    public ResultVO selectPermissionCascade() {

        List<CascadeVO> permissionCascadeList = permissionMapper.findPermissionCascade();

        return new ResultVO(true, "success", permissionCascadeList);
    }

    @Override
    public ResultVO saveAddPermission(SysPermission sysPermission) {
        if (sysPermission == null) {
            return new ResultVO(false, "参数不合法");
        }
        //通过父id查询子类权限
        List<SysPermission> childrenPermissionList = permissionMapper.findChildrenPermissionByParentId(sysPermission.getParentPerm());
        //如果查询的结果为空，则是第一次添加
        if (childrenPermissionList == null || childrenPermissionList.size() == 0) {
            int pid = Integer.parseInt(sysPermission.getParentPerm() + "01");
            sysPermission.setPermCode(String.valueOf(pid));
        } else {
            //定义数组
            String[] childrenIds = new String[childrenPermissionList.size()];
            //遍历集合,取出id放入数组
            for (int i = 0; i < childrenPermissionList.size(); i++) {
                childrenIds[i] = childrenPermissionList.get(i).getPermCode();
            }
            //查询的时候已经做了排序处理，取出最后一个id+1即可
            Integer newId = Integer.parseInt(childrenIds[childrenIds.length - 1]) + 1;

            sysPermission.setPermCode("0" + String.valueOf(newId));
        }
        //添加到数据库中
        int i = permissionMapper.insertPermission(sysPermission);

        return new ResultVO(i == 1 ? true : false, i == 1 ? "添加权限成功" : "添加权限失败");
    }

    @Override
    public ResultVO updatePermission(SysPermission sysPermission) {
        if (sysPermission == null) {
            return new ResultVO(false, "参数不合法");
        }

        int i = permissionMapper.updatePermissionInfo(sysPermission);

        return new ResultVO(i == 1 ? true : false, i == 1 ? "修改权限成功" : "修改权限失败");
    }

    @Override
    public ResultVO deletePermission(String permCode) {
        if (StringUtils.isEmpty(permCode)) {
            return new ResultVO(false, "参数不合法");
        }
        // 判断该节点下是否有子节点
        List<SysPermission> childrenPermissionList = permissionMapper.findChildrenPermissionByParentId(permCode);

        if (childrenPermissionList == null || childrenPermissionList.size() == 0) {
            // 该节点下没有子节点, 直接删除
            int i = permissionMapper.deletePermissionByPermCode(permCode);

            return new ResultVO(i == 1 ? true : false, i == 1 ? "删除权限成功" : "删除权限失败");
        } else {
            // 该节点下有子节点
            return new ResultVO(false, "该权限下有子权限，请您先删除子权限");
        }
    }
}
